namespace IFDB.Models.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    using IFDB.Models.Contexts;
    using IFDB.Models.Tables;
    using IFDB.ResourceLibrary;

    internal sealed class Configuration : DbMigrationsConfiguration<IFDBContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(IFDBContext db)
        {
            this.db = db;

            PopulateCompetitions();
            PopulateSeasons();
            PopulateTeams();

            db.SaveChanges();
        }

        IFDBContext db;

        #region Competitions
        private void PopulateCompetitions()
        {
            foreach (Competition competition in db.Competitions)
                db.Competitions.Remove(competition);

            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League", "Football League", 1, null, DateTime.Parse("01/07/1888"), DateTime.Parse("30/06/1892")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League First Division", "First Division", 1, null, DateTime.Parse("01/07/1892"), DateTime.Parse("30/06/1992")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Premier League", "Premier League", 1, null, DateTime.Parse("01/07/1992"), DateTime.Parse("31/12/9999")));

            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Second Division", "Second Division", 2, null, DateTime.Parse("01/07/1892"), DateTime.Parse("30/06/1992")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League First Division", "First Division", 2, null, DateTime.Parse("01/07/1992"), DateTime.Parse("30/06/2004")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Championship", "Championship", 2, null, DateTime.Parse("01/07/2004"), DateTime.Parse("31/12/9999")));

            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Third Division", "Third Division", 3, null, DateTime.Parse("01/07/1920"), DateTime.Parse("30/06/1921")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Third Division(North)", "Third Div. N", 3, null, DateTime.Parse("01/07/1921"), DateTime.Parse("30/06/1958")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Third Division(South)", "Third Div. S", 3, null, DateTime.Parse("01/07/1921"), DateTime.Parse("30/06/1958")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Third Division", "Third Division", 3, null, DateTime.Parse("01/07/1958"), DateTime.Parse("30/06/1992")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Second Division", "Second Division", 3, null, DateTime.Parse("01/07/1992"), DateTime.Parse("30/06/2004")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "League One", "League One", 3, null, DateTime.Parse("01/07/2004"), DateTime.Parse("31/12/9999")));

            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Fourth Division", "Fourth Division", 4, null, DateTime.Parse("01/07/1958"), DateTime.Parse("30/06/1992")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football League Third Division", "Third Division", 4, null, DateTime.Parse("01/07/1992"), DateTime.Parse("30/06/2004")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "League Two", "League Two", 4, null, DateTime.Parse("01/07/2004"), DateTime.Parse("31/12/9999")));

            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Alliance Premier League", "Alliance Prem.", 5, null, DateTime.Parse("01/07/1979"), DateTime.Parse("30/06/1987")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Football Conference", "Conference", 5, null, DateTime.Parse("01/07/1987"), DateTime.Parse("30/06/2004")));
            db.Competitions.Add(Competition.CreateNew(Country.England, CompetitionType.League, "Conference National", "Conference", 5, null, DateTime.Parse("01/07/2004"), DateTime.Parse("31/12/9999")));
        }
        #endregion

        #region Seasons
        private void PopulateSeasons()
        {
            foreach (Season season in db.Seasons)
                db.Seasons.Remove(season);

            db.Seasons.Add(Season.CreateNew(Country.England, "2007 / 2008", DateTime.Parse("01/08/2007"), DateTime.Parse("31/05/2008")));
            db.Seasons.Add(Season.CreateNew(Country.England, "2008 / 2009", DateTime.Parse("01/08/2008"), DateTime.Parse("31/05/2009")));
            db.Seasons.Add(Season.CreateNew(Country.England, "2009 / 2010", DateTime.Parse("01/08/2009"), DateTime.Parse("31/05/2010")));
            db.Seasons.Add(Season.CreateNew(Country.England, "2010 / 2011", DateTime.Parse("01/08/2010"), DateTime.Parse("31/05/2011")));
            db.Seasons.Add(Season.CreateNew(Country.England, "2011 / 2012", DateTime.Parse("01/08/2011"), DateTime.Parse("31/05/2012")));
            db.Seasons.Add(Season.CreateNew(Country.England, "2012 / 2013", DateTime.Parse("01/08/2012"), DateTime.Parse("31/05/2013")));
        }
        #endregion

        #region Teams and LookupTeams
        private void PopulateTeams()
        {
            foreach (LookupTeam lookupTeam in db.LookupTeams)
                db.LookupTeams.Remove(lookupTeam);

            foreach (TeamBadge teamBadge in db.TeamBadges)
                db.TeamBadges.Remove(teamBadge);

            foreach (TeamName teamName in db.TeamNames)
                db.TeamNames.Remove(teamName);

            foreach (Team team in db.Teams)
                db.Teams.Remove(team);

            AddTeam("Accrington Stanley", "2731", "42");
            AddTeam("AFC Bournemouth", "349", "359");
            AddTeam("AFC Telford United", "10183", "2546");
            AddTeam("AFC Wimbledon", "3802", "4194");
            AddTeam("Aldershot Town", "632", "92");
            AddTeam("Alfreton Town", "3165", "94");
            AddTeam("Arsenal", "359", "142", "england/Arsenal001.png");
            AddTeam("Aston Villa", "362", "154", "england/AstonVilla001.png");
            AddTeam("Barnet", "280", "344");
            AddTeam("Barnsley", "397", "208");
            AddTeam("Barrow", "642", "392");
            AddTeam("Birmingham City", "392", "291");
            AddTeam("Blackburn Rovers", "365", "308");
            AddTeam("Blackpool", "346", "317");
            AddTeam("Bolton Wanderers", "358", "354");
            AddTeam("Bradford City", "387", "234");
            AddTeam("Braintree Town", "3828", "423");
            AddTeam("Brentford", "337", "378");
            AddTeam("Brighton & Hove Albion", "331", "381");
            AddTeam("Bristol City", "333", "376");
            AddTeam("Bristol Rovers", "308", "388");
            AddTeam("Burnley", "379", "435");
            AddTeam("Burton Albion", "2567", "427");
            AddTeam("Bury", "354", "394");
            AddTeam("Cambridge United", "351", "483");
            AddTeam("Cardiff City", "347", "485");
            AddTeam("Carlisle United", "322", "643");
            AddTeam("Charlton Athletic", "372", "527");
            AddTeam("Chelsea", "363", "536", "england/Chelsea001.png");
            AddTeam("Cheltenham Town", "320", "579");
            AddTeam("Chesterfield", "343", "656");
            AddTeam("Colchester United", "339", "612");
            AddTeam("Coventry City", "388", "621");
            AddTeam("Crawley Town", "2594", "649");
            AddTeam("Crewe Alexandra", "396", "652");
            AddTeam("Crystal Palace", "384", "646");
            AddTeam("Dagenham & Redbridge", "275", "760");
            AddTeam("Dartford", "4236", "721");
            AddTeam("Derby County", "374", "747");
            AddTeam("Doncaster Rovers", "279", "800");
            AddTeam("Ebbsfleet United", "640", "1149");
            AddTeam("Everton", "368", "942", "england/Everton001.png");
            AddTeam("Exeter City", "324", "947");
            AddTeam("Fleetwood Town", "3891", "990");
            AddTeam("Forest Green Rovers", "282", "1048");
            AddTeam("Fulham", "370", "1055", "england/Fulham001.png");
            AddTeam("Gateshead", "3138", "1070");
            AddTeam("Gillingham", "389", "1098");
            AddTeam("Grimsby Town", "386", "1148");
            AddTeam("Hartlepool United", "323", "1190");
            AddTeam("Hereford United", "287", "1216");
            AddTeam("Huddersfield Town", "335", "1309");
            AddTeam("Hull City", "306", "1310");
            AddTeam("Hyde", "13298", "1332");
            AddTeam("Ipswich Town", "373", "1372");
            AddTeam("Kidderminster Harriers", "319", "1436");
            AddTeam("Leeds United", "357", "1524");
            AddTeam("Leicester City", "375", "1527");
            AddTeam("Leyton Orient", "309", "1537");
            AddTeam("Lincoln City", "314", "1559");
            AddTeam("Liverpool", "364", "1563", "england/Liverpool001.png");
            AddTeam("Luton Town", "301", "1628");
            AddTeam("Macclesfield Town", "316", "1660");
            AddTeam("Manchester City", "382", "1718", "england/ManchesterCity001.png");
            AddTeam("Manchester United", "360", "1724", "england/ManchesterUnited001.png");
            AddTeam("Mansfield Town", "304", "1723");
            AddTeam("Middlesbrough", "369", "1697");
            AddTeam("Millwall", "391", "1699");
            AddTeam("Milton Keynes Dons", "390", "2812");
            AddTeam("Morecambe", "278", "1732");
            AddTeam("Newcastle United", "361", "1823", "england/NewcastleUnited001.png");
            AddTeam("Newport County", "635", "1824");
            AddTeam("Northampton Town", "353", "1867");
            AddTeam("Norwich City", "381", "1855", "england/NorwichCity001.png");
            AddTeam("Nottingham Forest", "393", "1845");
            AddTeam("Notts County", "340", "1871");
            AddTeam("Nuneaton Town", "283", "1880");
            AddTeam("Oldham Athletic", "332", "1924");
            AddTeam("Oxford United", "311", "1964");
            AddTeam("Peterborough United", "342", "1996");
            AddTeam("Plymouth Argyle", "307", "2036");
            AddTeam("Port Vale", "348", "2083");
            AddTeam("Portsmouth", "385", "2049");
            AddTeam("Preston North End", "394", "2054");
            AddTeam("Queens Park Rangers", "334", "2093", "england/QueensParkRangers001.png");
            AddTeam("Reading", "338", "2125", "england/Reading001.png");
            AddTeam("Rochdale", "303", "2175");
            AddTeam("Rotherham United", "402", "2180");
            AddTeam("Scunthorpe United", "313", "2263");
            AddTeam("Sheffield United", "398", "2328");
            AddTeam("Sheffield Wednesday", "399", "2330");
            AddTeam("Shrewsbury Town", "302", "2325");
            AddTeam("Southampton", "376", "2471", "england/Southampton001.png");
            AddTeam("Southend United", "310", "2393");
            AddTeam("Southport", "286", "2432");
            AddTeam("Stevenage", "285", "2499");
            AddTeam("Stockport County", "400", "2473");
            AddTeam("Stoke City", "336", "2477", "england/StokeCity001.png");
            AddTeam("Sunderland", "366", "2493", "england/Sunderland001.png");
            AddTeam("Swansea City", "318", "2513", "england/SwanseaCity001.png");
            AddTeam("Swindon Town", "341", "2519");
            AddTeam("Tamworth", "645", "2537");
            AddTeam("Torquay United", "321", "2589");
            AddTeam("Tottenham Hotspur", "367", "2590", "england/TottenhamHotspur001.png");
            AddTeam("Tranmere Rovers", "345", "2598");
            AddTeam("Walsall", "401", "2737");
            AddTeam("Watford", "395", "2741");
            AddTeam("West Bromwich Albion", "383", "2744", "england/WestBromwichAlbion001.png");
            AddTeam("West Ham United", "371", "2802", "england/WestHamUnited001.png");
            AddTeam("Wigan Athletic", "350", "2783", "england/WiganAthletic001.png");
            AddTeam("Woking", "290", "2847");
            AddTeam("Wolverhampton Wanderers", "380", "2848");
            AddTeam("Wrexham", "352", "2859");
            AddTeam("Wycombe Wanderers", "344", "2898");
            AddTeam("Yeovil Town", "284", "2905");
            AddTeam("York City", "315", "2910");

        }

        private void AddTeam(string teamName, string espnId, string soccerbaseId, string badgePath = "")
        {
            Team newTeam = Team.CreateNew();
            db.Teams.Add(newTeam);

            db.TeamNames.Add(TeamName.CreateNew(teamName, newTeam.TeamId, _DateTime.LowDate, _DateTime.HighDate));

            if (badgePath != string.Empty)
                db.TeamBadges.Add(TeamBadge.CreateNew(badgePath, newTeam.TeamId, _DateTime.LowDate, _DateTime.HighDate));

            if (espnId != string.Empty)
                db.LookupTeams.Add(LookupTeam.CreateNew(Site.ESPN, espnId, newTeam.TeamId));

            if (soccerbaseId != string.Empty)
                db.LookupTeams.Add(LookupTeam.CreateNew(Site.Soccerbase, soccerbaseId, newTeam.TeamId));
        }
        #endregion
    }
}
